/**
 * 编码base64【同步方法】
 * @description 编码base64，得到编码后的字符串
 * @param {String,Number,Object} data 待编码的数据
 * @return {String} 编码后的字符串
 */
function encodeBase64(data = '') {
	
	// 初始化返回数据为false
	var data_return = false;
	
	// 将代码放入try，避免报错
	try {
		// ===================== data参数转换
		// 临时处理data的变量
		var data_mo = '';
		// 如果参数为string格式
		if (typeof(data)=="number") { data_mo = data.toString(); }
		// 如果参数为object格式
		else if (typeof(data)=="object") { data_mo = JSON.stringify(data); }
		// 其他情况等于原数据
		else if (typeof(data)=="string") { data_mo = data; }
		
		// ===================== 进行data参数解码
		// 转换后的参数有效，执行
		if (data_mo.length > 0)
		{
			// base64方法
			var Base64 = {_keyStr:"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",encode:function(r){var t,e,o,a,h,n,d,C="",i=0;for(r=Base64._utf8_encode(r);i<r.length;)a=(t=r.charCodeAt(i++))>>2,h=(3&t)<<4|(e=r.charCodeAt(i++))>>4,n=(15&e)<<2|(o=r.charCodeAt(i++))>>6,d=63&o,isNaN(e)?n=d=64:isNaN(o)&&(d=64),C=C+this._keyStr.charAt(a)+this._keyStr.charAt(h)+this._keyStr.charAt(n)+this._keyStr.charAt(d);return C},decode:function(r){var t,e,o,a,h,n,d="",C=0;for(r=r.replace(/[^A-Za-z0-9+/=]/g,"");C<r.length;)t=this._keyStr.indexOf(r.charAt(C++))<<2|(a=this._keyStr.indexOf(r.charAt(C++)))>>4,e=(15&a)<<4|(h=this._keyStr.indexOf(r.charAt(C++)))>>2,o=(3&h)<<6|(n=this._keyStr.indexOf(r.charAt(C++))),d+=String.fromCharCode(t),64!=h&&(d+=String.fromCharCode(e)),64!=n&&(d+=String.fromCharCode(o));return d=Base64._utf8_decode(d)},_utf8_encode:function(r){r=r.replace(/rn/g,"n");for(var t="",e=0;e<r.length;e++){var o=r.charCodeAt(e);o<128?t+=String.fromCharCode(o):o>127&&o<2048?(t+=String.fromCharCode(o>>6|192),t+=String.fromCharCode(63&o|128)):(t+=String.fromCharCode(o>>12|224),t+=String.fromCharCode(o>>6&63|128),t+=String.fromCharCode(63&o|128))}return t},_utf8_decode:function(r){for(var t,e,o="",a=0,h=0;a<r.length;)(t=r.charCodeAt(a))<128?(o+=String.fromCharCode(t),a++):t>191&&t<224?(e=r.charCodeAt(a+1),o+=String.fromCharCode((31&t)<<6|63&e),a+=2):(e=r.charCodeAt(a+1),h=r.charCodeAt(a+2),o+=String.fromCharCode((15&t)<<12|(63&e)<<6|63&h),a+=3);return o}};
			// 编码转换后的data数据
			data_return = Base64.encode(data_mo);
		}
	}
	// 如果报错，返回false
	catch (e) { data_return = false; }
	
	// 结果数据返回
	return data_return;
}

export default encodeBase64;